package com.itheima.prince.smartpeking.impl;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itheima.prince.smartpeking.activity.MainActivity;
import com.itheima.prince.smartpeking.base.BaseMenuPager;
import com.itheima.prince.smartpeking.base.BasePager;
import com.itheima.prince.smartpeking.bean.NewsCenterBean;
import com.itheima.prince.smartpeking.fragment.LeftFragment;
import com.itheima.prince.smartpeking.impl.NewsCenterPagers.InterractDetail;
import com.itheima.prince.smartpeking.impl.NewsCenterPagers.NewsCenterDetail;
import com.itheima.prince.smartpeking.impl.NewsCenterPagers.PhotoDetailPager;
import com.itheima.prince.smartpeking.impl.NewsCenterPagers.TopicDetailPager;
import com.itheima.prince.smartpeking.utils.ConstantUtil;
import com.itheima.prince.smartpeking.utils.SPUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Prince on 2016/10/20.
 */
public class NewsPager extends BasePager {
    public static final String CHAHE_NEWSCENTER_JSON = "cache_newscenter_json";
    private NewsCenterBean bean;
    private List<BaseMenuPager> baseMenuPagers;

    public NewsPager(Context context) {
        super(context);
    }

    @Override
    public void initdata() {
        Log.d("aaa", "新闻中心加载了a");
        mtitle.setText("新闻");

        //先取缓存数据
        String cacheJson = SPUtil.getString(mcontext, CHAHE_NEWSCENTER_JSON, "");
        if(!TextUtils.isEmpty(cacheJson)){
            parseJson(cacheJson);
        }
        getFromServer();

    }

    private void getFromServer() {
        OkHttpUtils
                .get()
                .url(ConstantUtil.NEWSCENTER_URL)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.d("aaa",e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.d("aaa",response);
                        //缓存新闻中心的数据
                        SPUtil.putString(mcontext,CHAHE_NEWSCENTER_JSON,response);
                        parseJson(response);
                    }


                });
    }

    public void parseJson(String json){
        Gson gson = new Gson();
        bean = gson.fromJson(json, NewsCenterBean.class);
        String title = bean.getData().get(0).getChildren().get(2).getTitle();
        Log.d("aaa",title);
        sendToLeft(bean.getData());
        baseMenuPagers = new ArrayList<>();
        //通过构造来传递数据
        baseMenuPagers.add(new NewsCenterDetail(mcontext,bean.getData().get(0).getChildren()));
        baseMenuPagers.add(new TopicDetailPager(mcontext));
        baseMenuPagers.add(new PhotoDetailPager(mcontext));
        baseMenuPagers.add(new InterractDetail(mcontext));

        //
        MainActivity mainActivity= (MainActivity) mcontext;
        LeftFragment leftFragment = mainActivity.getLeftFragment();

        switchMenuPager(leftFragment.currentIndex);
    }

    private void sendToLeft(List<NewsCenterBean.DataBean> data) {
        MainActivity mainActivity = (MainActivity) mcontext;
        android.support.v4.app.FragmentManager supportFragmentManager = mainActivity.getSupportFragmentManager();
        LeftFragment fragmentleft = (LeftFragment) supportFragmentManager.findFragmentByTag(MainActivity.LEFT_MENU);
        fragmentleft.setLeftContent(data);
        fragmentleft.initData();

    }

    public void switchMenuPager(int position) {
        //修改标题
        mtitle.setText(bean.getData().get(position).getTitle());
        mcontent.removeAllViews();
        mcontent.addView(baseMenuPagers.get(position).rootView);
        baseMenuPagers.get(position).initData();

        //组图界面的图片
        if (position==2){
            mGvOrLv.setVisibility(View.VISIBLE);
            final PhotoDetailPager photoDetailPager = (PhotoDetailPager) baseMenuPagers.get(2);
            mGvOrLv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //此方法不要写在这个页面
                    photoDetailPager.switchPhotoType(mGvOrLv);
                }
            });
        }else {
            mGvOrLv.setVisibility(View.GONE);
        }
    }

}
